package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.CampShowArea;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class CampShowAreaDAO {
	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(CampShowArea transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db
				+ ".Camp_Show_Area where Show_Area_Id="
				+ transientInstance.getShowAreaId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Camp_Show_Area set Camp_Id='"
						+ transientInstance.getCampId() + "',Show_Area_Name='"
						+ transientInstance.getShowAreaName()
						+ "',Show_Area_Desc='"
						+ transientInstance.getShowAreaDesc()
						+ "',Show_Order=" + transientInstance.getShowOrder()
						+ ",Creator_Name='"
						+ transientInstance.getCreatorName() + "',Create_Time=timestamp '"
						+ CurrentTime.getCurrentTime()
						+ "' where Show_Area_Id="
						+ transientInstance.getShowAreaId();
			else
				sql = "insert into " + db + ".Camp_Show_Area values(,'"
						+ transientInstance.getCampId() + "','"
						+ transientInstance.getShowAreaName() + "','"
						+ transientInstance.getShowAreaDesc() + "',"
						+ transientInstance.getShowOrder() + ",'"
						+ transientInstance.getCreatorName()+ "',timestamp '"
						+ CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(CampShowArea persistentInstance) {

		String sql = "delete from " + db
				+ ".Camp_Show_Area where Show_Area_Id = "
				+ persistentInstance.getShowAreaId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public CampShowArea findById(int id) {

		String sql = "select * from " + db
				+ ".Camp_Show_Area where Show_Area_Id = " + id;
		CampShowArea sd = new CampShowArea();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setShowAreaId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaName(rs.getString(3));
				sd.setShowAreaDesc(rs.getString(4));
				sd.setShowOrder(rs.getInt(5));
				sd.setCreatorName(rs.getString(6));
				sd.setCreateTime(rs.getString(7));
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return sd;

	}

	public List findAll() {

		String sql = "select * from " + db + ".Camp_Show_Area";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampShowArea sd = new CampShowArea();
				sd.setShowAreaId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaName(rs.getString(3));
				sd.setShowAreaDesc(rs.getString(4));
				sd.setShowOrder(rs.getInt(5));
				sd.setCreatorName(rs.getString(6));
				sd.setCreateTime(rs.getString(7));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}
	
	public List findAll(String campid) {

		String sql = "select * from " + db + ".Camp_Show_Area where camp_id="+campid;
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				CampShowArea sd = new CampShowArea();
				sd.setShowAreaId(rs.getInt(1));
				sd.setCampId(rs.getInt(2));
				sd.setShowAreaName(rs.getString(3));
				sd.setShowAreaDesc(rs.getString(4));
				sd.setShowOrder(rs.getInt(5));
				sd.setCreatorName(rs.getString(6));
				sd.setCreateTime(rs.getString(7));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		return list;

	}

}
